package com.virgo.demo.support.service.impl;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.utils.ITimeUtils;
import com.virgo.demo.support.domain.OperationLogDo;
import com.virgo.demo.support.domain.bo.OperationLogBo;
import com.virgo.demo.support.domain.vo.OperationLogVo;
import com.virgo.demo.support.mapper.OperationLogMapper;
import com.virgo.demo.support.service.OperationLogService;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLogDo> implements OperationLogService {

    private final OperationLogMapper operationLogMapper;

	/**
	 * 保存日志
	 * @param operationLogBo OperationLogBo
	 */
	@Override
	@Async
	public void saveLog(OperationLogBo operationLogBo) {
		OperationLogDo operationLogDo = new OperationLogDo();
		BeanUtils.copyProperties(operationLogBo,operationLogDo);
		UserAgent userAgentObject = UserAgentUtil.parse(operationLogDo.getUserAgent());
		operationLogDo.setUserAgent(userAgentObject.getBrowser().getName());
		operationLogDo.setOs(userAgentObject.getOs().getName());
		save(operationLogDo);
	}

    /**
     * 操作日志列表
     * @param request HttpServletRequest
     * @return ListDataVo<OperationLogVo>
     */
    @Override
    public ListDataVo<OperationLogVo> findList(HttpServletRequest request) {
        long currentPage = IRequestUtils.getLong(request,"page");
        long pageSize = IRequestUtils.getLong(request,"pageSize");
        currentPage = Math.max(currentPage,1);
        pageSize = Math.max(pageSize,10);

        HashMap<String, Object> params = new HashMap<>();

        long totalSize = operationLogMapper.countRows(params);
        if (totalSize > 0L) {
            long totalPage = (long) Math.ceil( (double) totalSize / pageSize );
            if (currentPage > totalPage) {
                return new ListDataVo<>(0L,0L,new ArrayList<>());
            }
            long offset = (currentPage - 1) * pageSize;
            params.put("offset", offset);
            params.put("limit", pageSize);
            List<OperationLogVo> operationLogVos = operationLogMapper.queryRows(params);
            for (OperationLogVo operationLogVo : operationLogVos) {
                operationLogVo.setCreatedTime(ITimeUtils.localDate(operationLogVo.getCreatedAt()));
                operationLogVo.setCreatedAt(0L);
            }
            return new ListDataVo<>(totalSize,totalPage,operationLogVos);
        }
        return new ListDataVo<>(0L,0L,new ArrayList<>());
    }
}